package com.lhgray.sglhome.presentation.ui.compose

import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.Button
import androidx.compose.material3.Text
import androidx.compose.material3.TextField
import androidx.compose.material3.TextFieldDefaults
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp

/**
 * 作者：lhgray on 2025/3/25.
 * 邮箱：lhgray@163.com
 * 文件：com.lhgray.sglhome.screen.compose
 */
@Preview(showBackground = true)
@Composable
fun InputPreviewView() {
    // InputSearch(TextFieldValue(), {}, {})
}

@Composable
fun InputSearch(
    initialText: String = "",
    onScanBarcode: () -> Unit,
    onSearchNumber: (String) -> Unit
) {
    var inputText by remember { mutableStateOf(initialText) }
    // 监听 initialText 的变化并更新 inputText
    LaunchedEffect(initialText) {
        inputText = initialText
    }
    Column(modifier = Modifier.padding(0.dp, 0.dp)) {
        // 输入框
        TextField(
            value = inputText,
            onValueChange = { inputText = it },
            placeholder = { Text("Please Enter HAWB # or Lot #") },
            modifier = Modifier
                .fillMaxWidth()
                .padding(horizontal = 16.dp),
            shape = RoundedCornerShape(16.dp),
            // 去除下横线
            colors = TextFieldDefaults.colors(
                focusedIndicatorColor = Color.Transparent,
                unfocusedIndicatorColor = Color.Transparent
            )
        )
        // 操作按钮
        Row(
            modifier = Modifier
                .fillMaxWidth()
                .padding(10.dp),
            horizontalArrangement = Arrangement.Center
        ) {
            Button(
                onClick = onScanBarcode,
                modifier = Modifier
                    .weight(1f)
                    .padding(8.dp),
                shape = RoundedCornerShape(8.dp)
            ) {
                Text("Scan Barcode")
            }
            Button(
                onClick = { onSearchNumber(inputText) },
                modifier = Modifier
                    .weight(1f)
                    .padding(8.dp),
                shape = RoundedCornerShape(8.dp)
            ) {
                Text("Search Number")
            }
        }
    }
}